package baitap.giaodien;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

class Bt_Giaodien_tinhhaiso extends JFrame {

	private static final long serialVersionUID = 1L;
	JPanel pn_main, pn1,pn2;
	JLabel lb_tinhtong, lb_so1, lb_so2;
	JTextField txt_so1, txt_so2, txt_kq;
	JButton bt_tong, bt_hieu, bt_tich, bt_thuong, bt_main,bt_new;
	JTextArea atxt1, atxt2;

	public Bt_Giaodien_tinhhaiso(String title) {
		super(title);

		lb_tinhtong = new JLabel("Tinh tong hai so nguyen");
		pn_main = new JPanel();
		pn1 = new JPanel();
		pn2 = new JPanel();
		
		
		
		TitledBorder tile_main = new TitledBorder("TitleBorder");
		tile_main.setTitleColor(Color.red);
		pn_main.setBorder(tile_main);
		TitledBorder tile_2 = new TitledBorder("Panel 2");
		pn2.setBorder(tile_2);
		// khai bao bien
		lb_so1 = new JLabel("So nguyen thu 1: ");
		lb_so2 = new JLabel("So nguyen thu 2: ");
		bt_tong = new JButton("Tong");
		bt_new = new JButton("BT_NEW");
		bt_main = new JButton("Main");
		bt_hieu = new JButton("Hieu");
		bt_tich = new JButton("Tich");
		bt_thuong = new JButton("Thuong");
		txt_so1 = new JTextField();
		txt_so2 = new JTextField();
		
		txt_kq = new JTextField("text");

		// mau cho sukien
		pn_main.setBackground(Color.black);
		pn1.setBackground(Color.CYAN);
		bt_hieu.setForeground(Color.blue);
		bt_hieu.setBackground(Color.CYAN);

		bt_tich.setForeground(Color.black);
		bt_tich.setBackground(Color.blue);

		bt_tong.setForeground(Color.green);
		bt_tong.setBackground(Color.pink);

		bt_thuong.setForeground(Color.RED);
		bt_thuong.setBackground(Color.pink);

		// set location
		lb_so1.setBounds(10, 50, 40, 20);

		pn_main.setBorder(tile_main);
		pn_main.setLayout(new GridLayout(4, 3));
		pn1.setLayout(new GridLayout(4, 2));
		pn2.setLayout(new GridLayout(1, 3));
		// pn_main.add(lb_tinhtong);
		pn1.add(lb_so1);
		pn1.add(txt_so1);
		pn1.add(lb_so2);
		pn1.add(txt_so2);
		pn1.add(bt_tong);
		pn1.add(bt_hieu);
		pn1.add(bt_tich);
		pn1.add(bt_thuong);

		pn_main.add(pn1);
		pn_main.add(pn2);
		pn2.add(txt_kq);
		pn2.add(bt_main);
		pn2.add(bt_new);
		this.add(pn_main);
		
		
		bt_tong.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Tong cua " + txt_so1.getText() + " + "
								+ txt_so2.getText() + " =  " + tinhtong());

			}
		});

		bt_hieu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Hieu cua " + txt_so1.getText() + " - "
								+ txt_so2.getText() + " =  " + tinhhieu());

			}
		});

		bt_tich.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Tich cua " + txt_so1.getText() + " X "
								+ txt_so2.getText() + " =  " + tinhtich());

			}
		});

		bt_thuong.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Thuong cua " + txt_so1.getText() + " / "
								+ txt_so2.getText() + " =  " + tinhthuong());

			}
		});
		bt_new.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Tong "+tinhtong()+"\n"+"Hieu: "+tinhhieu()+"\n"+"Tich: "+tinhtich()+"\n"+"Thuong: "+tinhthuong());

			}
		});
		bt_main.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {

				JOptionPane.showMessageDialog(
						null,
						"Main chua co nhap thong tin moi chi co tinh duoc tong hieu tich thuong thoi =)");

			}
		});

	}

	int tinhtong() {
		int ps1 = Integer.parseInt(txt_so1.getText());
		int ps2 = Integer.parseInt(txt_so2.getText());
		return ps1 + ps2;
	}

	int tinhhieu() {
		int ps1 = Integer.parseInt(txt_so1.getText());
		int ps2 = Integer.parseInt(txt_so2.getText());
		return ps1 - ps2;
	}

	int tinhtich() {
		int ps1 = Integer.parseInt(txt_so1.getText());
		int ps2 = Integer.parseInt(txt_so2.getText());
		return ps1 * ps2;
	}

	int tinhthuong() {
		int ps1 = Integer.parseInt(txt_so1.getText());
		int ps2 = Integer.parseInt(txt_so2.getText());
		return ps1 / ps2;
	}
}
